//
//  ImageAndTextCell.m
//  HiQ
//
//  Created by maxwell pang on 12-7-31.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//


#import "ImageAndTextCell.h"

#define kIconImageSize 32.0
#define kImageOriginXOffset 6.0
#define kTextOriginXOffset 10.0

@implementation ImageAndTextCell

- (void)dealloc {
    image = nil;
}

- copyWithZone:(NSZone *)zone
{
    ImageAndTextCell *cell = (ImageAndTextCell *)[super copyWithZone:zone];
    cell->image = image;
    return cell;
}

- (void)setImage:(NSImage *)anImage
{
    if (anImage != image)
	{
        image = anImage;
        [image setSize:NSMakeSize(kIconImageSize, kIconImageSize)];
    }
}

- (NSImage *)image
{
    return image;
}

- (NSRect)imageFrameForCellFrame:(NSRect)cellFrame
{
    if (image != nil)
	{
        NSRect imageFrame;
        imageFrame.size = [image size];
        imageFrame.origin = cellFrame.origin;
        imageFrame.origin.x += 3;
        imageFrame.origin.y += ceil((cellFrame.size.height - imageFrame.size.height) / 2);
        return imageFrame;
    }
    else
        return NSZeroRect;
}

- (void)editWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(NSText *)textObj delegate:(id)anObject event:(NSEvent *)theEvent
{
    NSRect textFrame, imageFrame;
    NSDivideRect (aRect, &imageFrame, &textFrame, 3 + [image size].width, NSMinXEdge);
    [super editWithFrame: textFrame inView: controlView editor:textObj delegate:anObject event: theEvent];
}

- (void)selectWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(NSText *)textObj delegate:(id)anObject start:(NSInteger)selStart length:(NSInteger)selLength
{
    NSRect textFrame, imageFrame;
    NSDivideRect (aRect, &imageFrame, &textFrame, 3 + [image size].width, NSMinXEdge);
    [super selectWithFrame: textFrame inView: controlView editor:textObj delegate:anObject start:selStart length:selLength];
}

- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
    NSRect imageRect = cellFrame;
    imageRect.origin.x += kImageOriginXOffset;
    imageRect.size = NSMakeSize(kIconImageSize, kIconImageSize);
    
    if (self.image) {
        [[NSGraphicsContext currentContext] saveGraphicsState];
        float yOffset = cellFrame.origin.y;
        if ([controlView isFlipped])
        {
            NSAffineTransform* xform = [NSAffineTransform transform];
            [xform translateXBy:0.0 yBy: cellFrame.size.height];
            [xform scaleXBy:1.0 yBy:-1.0];
            [xform concat];
            yOffset = 0-cellFrame.origin.y;
        }
        imageRect.origin.y = yOffset - 5.0;
        [self.image drawInRect:imageRect fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0 respectFlipped:NO hints:nil];
        [[NSGraphicsContext currentContext] restoreGraphicsState];
    }
    //draw string
    NSColor *fontColor = nil;
    NSAttributedString *arrStr;
    if (self.isHighlighted) {
        fontColor = [NSColor whiteColor];
        NSMutableParagraphStyle *pargStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
        [pargStyle setLineBreakMode:NSLineBreakByTruncatingTail];
        NSFont *font = [NSFont systemFontOfSize:13];
        NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:
                               fontColor, NSForegroundColorAttributeName,
                               font, NSFontAttributeName,
                               pargStyle, NSParagraphStyleAttributeName,
                               nil];
        arrStr = [[NSAttributedString alloc] initWithString:self.stringValue attributes:attrs];
    } else {
//        fontColor = [NSColor blackColor];
        arrStr = self.attributedStringValue;
    }
//    NSMutableParagraphStyle *pargStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
//    [pargStyle setLineBreakMode:NSLineBreakByTruncatingTail];
//	NSFont *font = [NSFont systemFontOfSize:13];
//	NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:
//                           fontColor, NSForegroundColorAttributeName,
//                           font, NSFontAttributeName,
//                           pargStyle, NSParagraphStyleAttributeName,
//                           nil];
//    NSAttributedString *arrStr = self.attributedStringValue;//[[NSAttributedString alloc] initWithString:self.stringValue attributes:attrs];
    
    NSRect textRect = cellFrame;
    textRect.origin.x = NSMaxX(imageRect) + kTextOriginXOffset;
    textRect.origin.y += ceil((cellFrame.size.height - arrStr.size.height) / 2);
    textRect.size.width = cellFrame.size.width - textRect.origin.x - 3.0;
    [arrStr drawInRect:textRect];
}

- (NSSize)cellSize
{
    NSSize cellSize = [super cellSize];
    cellSize.width += (image ? [image size].width : 0) + 3;
    return cellSize;
}

@end

